package com.guo.xiao.bing.controller;

import com.guo.xiao.bing.common.Config;
import com.qiniu.util.Auth;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("")
public class IndexController {

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/Uptoken", method = RequestMethod.POST)
    @ResponseBody
    public String getUptoken(String name,ModelMap model) {
        System.out.println("uptoken方法:"+name);
        // 获取七牛的uptoken
        String upToken=getUpToken();
        Map<String, String> map = new HashMap<String, String>();
        JSONObject json;
        map.put("upToken", upToken);
        json = JSONObject.fromObject(map);
        String check_return=json.toString();
        System.out.println("check_return="+check_return);
        return check_return;
    }



    private String getUpToken() {
        // 密钥配置
        System.out.println("初始化上传变量");
        Auth auth = Auth.create(Config.ACCESS_KEY, Config.SECRET_KEY);
        return auth.uploadToken(Config.bucketname);
    }
}
